package hhm.android.main;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import hhm.android.library.calendar.DateUtil;
import hhm.android.library.datePicker.DateTimePickerView;
import hhm.android.library.datePicker.bean.DateParams;

public class SelectDateSpaceBottomSheet extends BottomSheetDialog {
    private final Context context;
    final int SEVEN_DAY = 0;
    final int ONE_MONTH = 1;
    final int THREE_MONTH = 2;
    final int ONE_YEAR = 3;
    final int SELF_DIY_START = 4;
    final int SELF_DIY_END = 5;
    private int topSelect = THREE_MONTH;
    private int selectState = SELF_DIY_START;
    private long startTime;
    private String sStartTime;
    private long endTime;
    private String sEndTime;
    private final SelectDateSpaceBottomSheetListener listener;
    private TextView btn1;
    private TextView btn2;
    private TextView btn3;
    private TextView btn4;
    private TextView btn;

    public SelectDateSpaceBottomSheet(@NonNull Context context, SelectDateSpaceBottomSheetListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        createView();
    }

    public void createView() {
        View bottomSheetView = getLayoutInflater().inflate(R.layout.item_bottom_select_time, null);
        initView(bottomSheetView);

        setContentView(bottomSheetView);

        // 注意：这里要给layout的parent设置peekHeight，而不是在layout里给layout本身设置，下面设置背景色同理，坑爹！！！
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(((View) bottomSheetView.getParent()));
        bottomSheetBehavior.setPeekHeight(dp2px(578f));
        //允许点击外部关闭，不允许滑动关闭
        bottomSheetBehavior.setHideable(false);

        ((View) bottomSheetView.getParent()).setBackgroundColor(context.getResources().getColor(R.color.trans));
    }

    private void initView(View v) {
        DateTimePickerView dateTimePickerView = v.findViewById(R.id.item_bottom_select_time_date);
        initDateTimePickerView(dateTimePickerView);

        LinearLayout llStart = v.findViewById(R.id.item_bottom_select_time_start);
        TextView llStartTv = v.findViewById(R.id.item_bottom_select_time_start_tv);
        View llStartLine = v.findViewById(R.id.item_bottom_select_time_start_line);
        LinearLayout llEnd = v.findViewById(R.id.item_bottom_select_time_end);
        TextView llEndTv = v.findViewById(R.id.item_bottom_select_time_end_tv);
        View llEndLine = v.findViewById(R.id.item_bottom_select_time_end_line);
        btn1 = v.findViewById(R.id.item_bottom_select_time_btn1);
        btn2 = v.findViewById(R.id.item_bottom_select_time_btn2);
        btn3 = v.findViewById(R.id.item_bottom_select_time_btn3);
        btn4 = v.findViewById(R.id.item_bottom_select_time_btn4);
        btn = v.findViewById(R.id.item_bottom_select_time_btn);

        selectTop(topSelect);

        llStart.setOnClickListener(v1 -> {
            selectState = SELF_DIY_START;
            llStartTv.setTextColor(ContextCompat.getColor(context, R.color.mainTextColor));
            llStartLine.setBackgroundColor(ContextCompat.getColor(context, R.color.mainColor));
            llEndTv.setTextColor(ContextCompat.getColor(context, R.color.color_777777));
            llEndLine.setBackgroundColor(ContextCompat.getColor(context, R.color.color_777777));
        });
        llEnd.setOnClickListener(v1 -> {
            selectState = SELF_DIY_END;
            llEndTv.setTextColor(ContextCompat.getColor(context, R.color.mainTextColor));
            llEndLine.setBackgroundColor(ContextCompat.getColor(context, R.color.mainColor));
            llStartTv.setTextColor(ContextCompat.getColor(context, R.color.color_777777));
            llStartLine.setBackgroundColor(ContextCompat.getColor(context, R.color.color_777777));
        });
        dateTimePickerView.setOnChangeListener(date -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            if (selectState == SELF_DIY_START) {
                llStartTv.setText(format);
                startTime = date.getTime();
                sStartTime = format;
            } else {
                llEndTv.setText(format);
                endTime = date.getTime();
                sEndTime = format;
            }
        });

        btn.setOnClickListener(v12 -> {
            cancel();
            if (startTime >= endTime) {
                Toast.makeText(context, context.getString(R.string.start_time_must_bigger_than_end_time), Toast.LENGTH_SHORT).show();
                return;
            }
            String sStart = sStartTime.replaceFirst("-", "年").replaceFirst("-", "月");
            sStart += "日-";
            String sEnd = sEndTime.replaceFirst("-", "年").replaceFirst("-", "月");
            sEnd += "日";
            listener.select(startTime, endTime, sStart + sEnd);
        });

        v.findViewById(R.id.item_bottom_select_time_x).setOnClickListener(v1 -> cancel());
        btn1.setOnClickListener(v1 -> {
            cancel();
            selectTop(SEVEN_DAY);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date endDate = new Date();
            long end = endDate.getTime();
            String sEnd = sdf.format(endDate);
            Date startDate = DateUtil.addDay(endDate, -7);
            String sStart = sdf.format(startDate);
            listener.select(startDate.getTime(), end, sStart + "-" + sEnd);
        });
        v.findViewById(R.id.item_bottom_select_time_btn2).setOnClickListener(v1 -> {
            cancel();
            selectTop(ONE_MONTH);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date endDate = new Date();
            long end = endDate.getTime();
            String sEnd = sdf.format(endDate);
            Date startDate = DateUtil.addMonth(endDate, -1);
            String sStart = sdf.format(startDate);
            listener.select(startDate.getTime(), end, sStart + "-" + sEnd);
        });
        v.findViewById(R.id.item_bottom_select_time_btn3).setOnClickListener(v1 -> {
            cancel();
            selectTop(THREE_MONTH);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date endDate = new Date();
            long end = endDate.getTime();
            String sEnd = sdf.format(endDate);
            Date startDate = DateUtil.addMonth(endDate, -3);
            String sStart = sdf.format(startDate);
            listener.select(startDate.getTime(), end, sStart + "-" + sEnd);
        });
        v.findViewById(R.id.item_bottom_select_time_btn4).setOnClickListener(v1 -> {
            cancel();
            selectTop(ONE_YEAR);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            Date endDate = new Date();
            long end = endDate.getTime();
            String sEnd = sdf.format(endDate);
            Date startDate = DateUtil.addYear(endDate, -1);
            String sStart = sdf.format(startDate);
            listener.select(startDate.getTime(), end, sStart + "-" + sEnd);
        });

    }

    private void selectTop(int pos) {
        btn1.setBackgroundResource(R.drawable.rectangle_ring_grey_a);
        btn2.setBackgroundResource(R.drawable.rectangle_ring_grey_a);
        btn3.setBackgroundResource(R.drawable.rectangle_ring_grey_a);
        btn4.setBackgroundResource(R.drawable.rectangle_ring_grey_a);
//        switch (pos) {
//            case 0:
//                btn1.setBackgroundResource(R.drawable.rectangle_ring_maincolor);
//                break;
//            case 1:
//                btn2.setBackgroundResource(R.drawable.rectangle_ring_maincolor);
//                break;
//            case 2:
//                btn3.setBackgroundResource(R.drawable.rectangle_ring_maincolor);
//                break;
//            case 3:
//                btn4.setBackgroundResource(R.drawable.rectangle_ring_maincolor);
//                break;
//        }
        topSelect = pos;
    }

    private void initDateTimePickerView(DateTimePickerView dateTimePickerView) {
        Calendar todayCal = Calendar.getInstance();
        Calendar startCal = Calendar.getInstance();
        Calendar endCal = Calendar.getInstance();
        startCal.add(Calendar.YEAR, -99);
        endCal.add(Calendar.YEAR, 1);

        DateParams dateParams = new DateParams();
        dateParams.types = new int[]{DateParams.TYPE_YEAR, DateParams.TYPE_MONTH, DateParams.TYPE_DAY};
        dateParams.currentDate = todayCal.getTime();
        dateParams.startDate = startCal.getTime();
        dateParams.endDate = endCal.getTime();
        dateTimePickerView.show(dateParams, ContextCompat.getColor(context, R.color.color_333333));
    }

    public int dp2px(Float dpValue) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics()));
    }

    public interface SelectDateSpaceBottomSheetListener {
        void select(long start, long end, String text);
    }
}